package com.goldenautumn.prefabricate.business.mall

import android.app.Application
import android.util.SparseArray
import androidx.annotation.NonNull
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.business.entity.AddCarGoodsParams
import com.goldenautumn.prefabricate.business.entity.AddressInfoVo
import com.goldenautumn.prefabricate.business.entity.CarGoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.HomeItemDataVo
import com.goldenautumn.prefabricate.business.entity.SearchParams
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import qncx.external.common.Global
import qncx.external.common.base.BaseViewModel

class MallViewModel (application: Application) : BaseViewModel(application) {

    private val mRepository: MallRepositoryImpl = MallRepositoryImpl()

    val goodsDetailsRequests: Channel<GoodsInfoUiState> = Channel(Channel.UNLIMITED)

    val homeDataReq: Channel<HomeDataUiState> = Channel(Channel.UNLIMITED)

    val loading: Channel<LoadingUiState> = Channel(Channel.UNLIMITED)

    val carDataReq: Channel<CarDataUiState> = Channel(Channel.UNLIMITED)

   fun getHomeData(){
//       getHomeTab()
       viewModelScope.launch {
           launchFlow(errorCall = {
           }, requestCall = {
               mRepository.getHomeData()
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               val bannerInfo = info.items?.findLast { it.type == "banner" }
               val navBarInfo = info.items?.findLast { it.type == "navBar" }
               val goodsHit = info.items?.findLast { it.type == "goods" && it.style?.display == "slide" }
               val goodsList = info.items?.findLast { it.type == "goods" && it.style?.display == "list" }
               bannerInfo?.data?.let {
                   val mapData:MutableList<HomeItemDataVo> = GsonUtils.fromJson(it.toString(),object : TypeToken<MutableList<HomeItemDataVo>>() {}.type)
                   homeDataReq.trySend(HomeDataUiState.BannerData(mapData))
               }
               navBarInfo?.data?.let {
                   val mapData:MutableList<HomeItemDataVo> = GsonUtils.fromJson(it.toString(),object : TypeToken<MutableList<HomeItemDataVo>>() {}.type)
                   homeDataReq.trySend(HomeDataUiState.HomeMenu(mapData))
               }
               goodsHit?.data?.let {
                   val mapData:MutableList<HomeItemDataVo> = GsonUtils.fromJson(it.toString(),object : TypeToken<MutableList<HomeItemDataVo>>() {}.type)
                   homeDataReq.trySend(HomeDataUiState.HomeHit(mapData))
               }
               goodsList?.data?.let {
                   if(it.isJsonObject){
                       val data = it.asJsonObject
                       goodsList.brand?.forEach { tab ->
                           val list = data.get(tab.index)
                           if(list.isJsonArray) {
                               val mapData:MutableList<HomeItemDataVo> = GsonUtils.fromJson(list.toString(),object : TypeToken<MutableList<HomeItemDataVo>>() {}.type)
                               tab.data = mapData
                           }
                       }
                       goodsList.brand?.let { homeDataReq.trySend(HomeDataUiState.HomeTab(it)) }
                   }
               }
           }
       }
   }

    fun getHomeTab(){
       viewModelScope.launch {
           launchFlow(errorCall = {
           }, requestCall = {
               mRepository.getHomeTab()
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               homeDataReq.trySend(HomeDataUiState.HomeTab(info))
           }
       }
   }

    fun getGoodsList(params:SearchParams){
       viewModelScope.launch {
           launchFlow(errorCall = {
           }, requestCall = {
               mRepository.getGoodsList(params)
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               info.list?.data?.let { goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsList(it)) }
           }
       }
   }

    fun getGoodsDetails(id:String){
       viewModelScope.launch {
           launchFlow(errorCall = {  }, requestCall = {
               mRepository.getGoodsDetails(id)
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               info.detail?.let {
                    it.goods_multi_spec?.spec_attr?.forEach {  p ->
                        p.spec_items?.forEach { itemSpec ->
                            val item = it.goods_multi_spec?.spec_list?.findLast { it.spec_sku_id == itemSpec.item_id }
                            itemSpec.goods_price = item?.form?.goods_price
                        }
                    }
                   goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsDetails(it))
               }
           }
       }
   }
    fun addCar(goodsID:String,count:String,sku:String){
       viewModelScope.launch {
           launchFlow(errorCall = {
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(it))
           }, requestCall = {
               mRepository.onAddVarGoods(AddCarGoodsParams(
                   goods_id = goodsID,
                   goods_num = count,
                   goods_sku_id = sku,
                   s = "api/cart/add"
               ))
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(null))
           }
       }
   }

    fun addCarBatch(goods:List<GoodsInfoVO>){
       viewModelScope.launch {
           launchFlow(errorCall = {
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(it))
           }, requestCall = {
               goods.forEach { info ->
                   mRepository.onAddVarGoods(AddCarGoodsParams(
                       goods_id = "${info.goods_id}",
                       goods_num = "${info.total_num}",
                       goods_sku_id = info.goods_sku?.spec_sku_id ?: "0",
                       s = "api/cart/add"
                   ))
               }
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(null))
           }
       }
   }

    fun subCar(goodsID:String,count:String,sku:String){
       viewModelScope.launch {
           launchFlow(errorCall = {
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(it))
           }, requestCall = {
               mRepository.subCarGoods(AddCarGoodsParams(
                   goods_id = goodsID,
                   goods_num = count,
                   goods_sku_id = sku,
                   s = "api/cart/sub"
               ))
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               goodsDetailsRequests.trySend(GoodsInfoUiState.GoodsAddCar(null))
           }
       }
   }

    fun getCarList(){
       viewModelScope.launch {
           launchFlow(errorCall = {

           }, requestCall = {
               mRepository.getCarList()
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               carDataReq.trySend(CarDataUiState.CarList(info.goods_list))
           }
       }
   }

    fun onDeleteCar(skuID:String,deleteData:List<CarGoodsInfoVO>){
       viewModelScope.launch {
           launchFlow(errorCall = {
               carDataReq.trySend(CarDataUiState.CarDelete(it,deleteData))
           }, requestCall = {
               mRepository.deleteCarData(skuID)
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
               carDataReq.trySend(CarDataUiState.CarDelete(null,deleteData))
           }
       }
   }

    fun getCategoryList(){
       viewModelScope.launch {
           launchFlow(errorCall = {}, requestCall = {
               mRepository.getCategoryList()
           }, {
               loading.trySend(LoadingUiState.LoadingUI(it))
           }) { info ->
              info.list?.let {
                  homeDataReq.trySend(HomeDataUiState.CategoryGoods(it))
              }
           }
       }
   }

}